﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using My_TV_Manager.View.MenuBar;

namespace My_TV_Manager.View.ContentPanel
{
    [System.ComponentModel.DesignerCategory("")]

    public class OuterTabbedContainer : SplitContainer
    {
        #region Private Data

        // Interface components
        private OuterMenuBar outerMenuBar;
        private List<InnerTabbedContainer> innerTabbedContainers;

        // Manage size of the menu bar
        private double menuBarPercentage;
        private int menuBarMinimumWidth;

        // Color scheme of the application
        private ColorScheme colorScheme;

        private Controller controller;

        #endregion

        #region Properties

        public double MenuBarPercentage
        {
            get { return menuBarPercentage; }
            set
            {
                if (value > 1.0) { menuBarPercentage = value / 100.0f; }
                else if (value > 0.0) { menuBarPercentage = value; }
                else { value = 0.1; }
            }
        }

        public int MenuBarMinimumWidth
        {
            get { return menuBarMinimumWidth; }
            set { menuBarMinimumWidth = value; }
        }

        public ColorScheme ColorScheme
        {
            get { return colorScheme; }
            set { colorScheme = value; }
        }

        #endregion

        #region Constructors

        // Constructor using controller for program data
        public OuterTabbedContainer(Size size, ColorScheme colorScheme, Controller controller)
        {
            // Use the data parameters
            this.Size = size;
            this.colorScheme = colorScheme;
            this.controller = controller;

            // Format the menu bar width
            menuBarPercentage = 0.125;
            menuBarMinimumWidth = 100;
            this.SplitterDistance = (int)(this.ClientSize.Width * menuBarPercentage);
            if (this.SplitterDistance < menuBarMinimumWidth)
            {
                this.SplitterDistance = menuBarMinimumWidth;
            }

            // Format the split container
            this.Dock = DockStyle.Fill;
            this.Orientation = Orientation.Vertical;
            this.IsSplitterFixed = true;
            this.FixedPanel = FixedPanel.Panel1;
            this.SplitterWidth = 1;
            this.BorderStyle = BorderStyle.None;

            // Create and format the menu options tool strip
            var buttons = new List<string>()
            {
                "manage", 
                "track", 
                "organize", 
                "analyze", 
                "settings"
            };

            // Add the content panels to the main content window
            innerTabbedContainers = new List<InnerTabbedContainer>();
            innerTabbedContainers.Add(new ManageTab(this.Panel2.ClientSize, 0, controller));
            innerTabbedContainers.Add(new TrackTab(this.Panel2.ClientSize, 1, controller));
            innerTabbedContainers.Add(new OrganizeTab(this.Panel2.ClientSize, 2, controller));
            innerTabbedContainers.Add(new AnalyzeTab(this.Panel2.ClientSize, 3, controller));
            innerTabbedContainers.Add(new SettingsTab(this.Panel2.ClientSize, 4, controller));
            this.Panel2.Controls.Add(innerTabbedContainers[0]);

            // Add the menu bar to the main content window
            outerMenuBar = new OuterMenuBar(this, this.Panel1.ClientSize, buttons, controller);
            this.Panel1.Controls.Add(outerMenuBar);

            this.Resize += new EventHandler(WindowResizeEvent);
        }

        #endregion

        #region Event Handlers

        private void WindowResizeEvent(object sender, EventArgs e)
        {
            this.Panel1MinSize = (int)(this.ClientSize.Width * menuBarPercentage);
            if (this.Panel1MinSize < menuBarMinimumWidth)
            {
                this.Panel1MinSize = menuBarMinimumWidth;
            }

            outerMenuBar.ContainerSize = new Size(this.Panel1MinSize, this.Height);
        }

        #endregion

        #region Functions

        public void ChangeContentContainer(int index)
        {
            if (innerTabbedContainers.Count < index ||
                index < 0) { return; }
            
            this.Panel2.Controls.RemoveAt(0);
            this.Panel2.Controls.Add(innerTabbedContainers[index]);
        }

        #endregion
    }
}
